package util;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

import android.database.Cursor;
import android.view.View;
import android.widget.AdapterView;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;

import database.DatabaseAdapter;

public class Generate {
	static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	

	public static String getGuid(){
		String DATE_FORMAT = "yyyyMMddHHmmSSS";
	    SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
	    Calendar c1 = Calendar.getInstance(Locale.UK); // today
	    String ran = "";
	    for (int i=0;i<5;i++){
	    	int int_ran = (int) (Math.random()*10);
	    	ran += int_ran;
	    //	System.out.println(ran);
	    }
	    
	    
	    //return "111";
	    return ( sdf.format(c1.getTime())+ran);
		
	}
	
	public static String getACDID(){
		String DATE_FORMAT = "yyyyMMddHHmm";
	    SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
	    Calendar c1 = Calendar.getInstance(Locale.UK); // today
	   
	    
	    
	    //return "111";
	    return ( sdf.format(c1.getTime()));
		
	}
	
	public static String getPicGroupID(){
		String []arr_str = {"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
	    Calendar c1 = Calendar.getInstance(Locale.UK); // today
	    
	    int year = c1.get(Calendar.YEAR);
	    int month = c1.get(Calendar.MONTH);
	    int date = c1.get(Calendar.DATE);
	    int hour = c1.get(Calendar.HOUR_OF_DAY);
	    int min = c1.get(Calendar.MINUTE);
	    //int ss = c1.get(Calendar.SECOND);
	    String ran = "";
	    
	    ran += arr_str[(year-2000)];
	    ran += arr_str[month+1];
	    ran += arr_str[date];
	    ran += arr_str[hour];
	    ran += arr_str[min];
	    for (int i=0;i<5;i++){
	    	int int_ran = (int) (Math.random()*36);
	    	ran += arr_str[int_ran];
	    }
	    return ran;
		
	}
	public static String getStringFromCalendar(Calendar calendar){
		
		String strdate = "";
		if (calendar != null) {
			strdate = dateFormat.format(calendar.getTime());
		}
		return strdate;
	}
	
	@SuppressWarnings("null")
	public static Calendar getCalendarFromString(String str){
		Calendar cal = null;
	    try {
			cal.setTime(dateFormat.parse(str));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
		}
		return cal;
	}
	
	public static String getDateShowFormString(String dateval){
		String str ="-";
		try{
			String str_year = dateval.substring(0,4);
			String str_month = DefineValue.getMonth2()[Integer.parseInt(dateval.substring(4,6))-1];
			String str_date = dateval.substring(6,8);
			str = str_date+" "+str_month+" "+str_year;
			
		}catch(Exception ex){
			
		}
		return str;
		
	}
	
	public static String getStringData(String data){
		return (data == null ||"".equals(data)||"null".equals(data))?"":data;
	}
	
	public static String getStringDateNow(){
		String DATE_FORMAT = "yyyyMMdd";
	    SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
	    Calendar c1 = Calendar.getInstance(Locale.UK); // today
	    //return "111";
	    return ( sdf.format(c1.getTime()));
		
	}
	
	public static String md5(String input) {
	    String result = input;
	    if(input != null) {
	        MessageDigest md = null;
			try {
				md = MessageDigest.getInstance("MD5");
			} catch (NoSuchAlgorithmException e) {
				// TODO Auto-generated catch block
				return "";
			} //or "SHA-1"
	        md.update(input.getBytes());
	        BigInteger hash = new BigInteger(1, md.digest());
	        result = hash.toString(16);
	        while(result.length() < 32) {
	            result = "0" + result;
	        }
	    }
	    return result;
	}
	
	
	
	
	
	
	
	

}
